<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>对象(object)</title>
</head>

<body>
    <script>
        /*
        let dog = new Object();
        dog.name = "捷克狼";
        dog.age = 2;
        dog.say = function () {
            console.log("狗叫");
        };
        console.log(dog);
        */

        /*
        let dog = {
            name: "捷克狼",
            age: 2,
            say: function () {
                console.log("狗叫");
            },
        }
        console.log(dog);
        */

        /*
        function dog(name, age, say) {
            this.name = name;
            this.age = age;
            this.say = say;
        }

        在js中，this通常指向的是我们正在执行的函数本身，或者是指向该函数所属的对象(运行时)

        let myDog = new dog("捷克狼", 2, function () {
            console.log("狗叫");
        }
        );
        console.log(myDog);
        */

        /*
        function dog(name, age, say) {
            this.name = name;
            this.age = age;
            this.say = say;
        }

        let myDog = new dog("捷克狼", 2, function () {
            console.log("狗叫");
        });
        console.log(myDog.name);
        console.log(myDog['say']()); */

        /*
        let obj = {};
        obj[123] = 456;
        console.log(obj['123']); 
        */


        /*
        let obj = {
            name: "czh",
            age: 22
        }
        let val = 'name';
        console.log(obj[val]);
        console.log(obj.haha);
        //undefined
        console.log(obj.haha.heihei);
        //报错 
        */

        /*
        let person = {}
        person.name = "czh";
        person.age = 22;
        person.say = function () {
            console.loh("hai");
        }
        console.log(person);
        console.log(person.age);
        person.age = 9999;
        console.log(person.age);
        */

        /*
        let person = {
            name: 'bigFlower',
            age: 99999,
            say: function () {
                console.log("小猫");
            },
        };
        for (key in person) {
            console.log(key, person[key]);
        }
        //key是键名,obj[key]是键值
        */

        /*
        let person = {
            name: 'bigFlower',
            age: 99999,
            say: function () {
                console.log("小猫");
            },
        };
        for (key in person) {
            delete person.name;
            console.log(person, person.name);
        }
        */

        /* let obj = { name: "jack" };
        console.log("name" in obj);
        //true
        console.log("age" in obj);
        //fales
        */

        /* let obj = {
            name: 'czh',
            age: 22,
            sayName: function () {
                console.log(this.name);
            }
        };
        let obj1 = {
            name: 'alex',
            age: 20,
            sayName: function () {
                console.log(this.name);
            }
        };
        let obj2 = {
            name: '花',
            age: 100,
            sayName: function () {
                console.log(this.name);
            }
        };
        */

        /*
        使用工厂的方法创建对象
        该方法可以批量创建对象

        function createPerson(name, age) {
            let obj = new Object();
            obj.name = name;
            obj.age = age;
            obj.sayName = function () {
                console.log(this.name);
            };
            return obj;
        }
        let obj1 = createPerson("czh", 18);
        let obj2 = createPerson("alex", 20);
        let obj3 = createPerson("花", 999);
        console.log(obj1, obj2, obj3.sayName());
        */

        /* function add() {
            console.log(this);
            //window
            function addin() {
                console.log(this);
                //window
            }
            addin();
        }
        add();
        
        以方法的形式调用
        
        let obj = {
            name: 'hello',
            myfun: function () {
                console.log(this);
            },
        };
        obj.myfun();//obj
        
        
        以构造函数的形式调用
        

        function Person(name, age, greder) {
            this.name = name;
            this.age = age;
            this.greder = greder;
            this.showThis = function () {
                console.log(this);
            };
        }
        let per1 = new Person('czh', 22, '男');
        let per2 = new Person('clw', 22, '女');
        console.log(per1.showThis(), per2.showThis());
        */


        /* function Person(name, age) {
            this.name = name;
            this.age = age;
        }
        let per = new Person('花', 999)
        function Animal() { }
        let dog = new Animal();
        console.log(per instanceof Person);
        //true

        console.log(dog instanceof Animal);
        //true

        console.log(per instanceof Animal);
        //false 

        需要注意的是所有的对象都是Object的后代, 任何对象和Object做instanceof检查到的结果都是true 

        console.log(per instanceof Object); */


        function Person(name, age) {
            this.name = name;
            this.age = age;
            /* this.sayName = function () {
                console.log(this.name);
            */
            this.sayName = sayName;
        }

        function sayName() {
            console.log(this.name);
        }
        let per = new Person('czh', 22);
        let per1 = new Person('clw', 22);
        console.log(per.sayName(), per1.sayName());//czh clw
        console.log(per.sayName === per1.sayName);//true 
    </script>
</body>

</html>